package cn.gemiman.daoyi.module.cms.controller.admin.articledetailreadlogs;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.gemiman.daoyi.framework.common.pojo.PageParam;
import cn.gemiman.daoyi.framework.common.pojo.PageResult;
import cn.gemiman.daoyi.framework.common.pojo.CommonResult;
import cn.gemiman.daoyi.framework.common.util.object.BeanUtils;
import static cn.gemiman.daoyi.framework.common.pojo.CommonResult.success;

import cn.gemiman.daoyi.framework.excel.core.util.ExcelUtils;

import cn.gemiman.daoyi.framework.apilog.core.annotation.ApiAccessLog;
import static cn.gemiman.daoyi.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.gemiman.daoyi.module.cms.controller.admin.articledetailreadlogs.vo.*;
import cn.gemiman.daoyi.module.cms.dal.dataobject.articledetailreadlogs.ArticleDetailReadLogsDO;
import cn.gemiman.daoyi.module.cms.service.articledetailreadlogs.ArticleDetailReadLogsService;

@Tag(name = "管理后台 - 阅读记录")
@RestController
@RequestMapping("/cms/article-detail-read-logs")
@Validated
public class ArticleDetailReadLogsController {

    @Resource
    private ArticleDetailReadLogsService articleDetailReadLogsService;

    @PostMapping("/create")
    @Operation(summary = "创建阅读记录")
    @PreAuthorize("@ss.hasPermission('cms:article-detail-read-logs:create')")
    public CommonResult<Long> createArticleDetailReadLogs(@Valid @RequestBody ArticleDetailReadLogsSaveReqVO createReqVO) {
        return success(articleDetailReadLogsService.createArticleDetailReadLogs(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新阅读记录")
    @PreAuthorize("@ss.hasPermission('cms:article-detail-read-logs:update')")
    public CommonResult<Boolean> updateArticleDetailReadLogs(@Valid @RequestBody ArticleDetailReadLogsSaveReqVO updateReqVO) {
        articleDetailReadLogsService.updateArticleDetailReadLogs(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除阅读记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('cms:article-detail-read-logs:delete')")
    public CommonResult<Boolean> deleteArticleDetailReadLogs(@RequestParam("id") Long id) {
        articleDetailReadLogsService.deleteArticleDetailReadLogs(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得阅读记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('cms:article-detail-read-logs:query')")
    public CommonResult<ArticleDetailReadLogsRespVO> getArticleDetailReadLogs(@RequestParam("id") Long id) {
        ArticleDetailReadLogsDO articleDetailReadLogs = articleDetailReadLogsService.getArticleDetailReadLogs(id);
        return success(BeanUtils.toBean(articleDetailReadLogs, ArticleDetailReadLogsRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得阅读记录分页")
    @PreAuthorize("@ss.hasPermission('cms:article-detail-read-logs:query')")
    public CommonResult<PageResult<ArticleDetailReadLogsRespVO>> getArticleDetailReadLogsPage(@Valid ArticleDetailReadLogsPageReqVO pageReqVO) {
        PageResult<ArticleDetailReadLogsDO> pageResult = articleDetailReadLogsService.getArticleDetailReadLogsPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ArticleDetailReadLogsRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出阅读记录 Excel")
    @PreAuthorize("@ss.hasPermission('cms:article-detail-read-logs:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportArticleDetailReadLogsExcel(@Valid ArticleDetailReadLogsPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ArticleDetailReadLogsDO> list = articleDetailReadLogsService.getArticleDetailReadLogsPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "阅读记录.xls", "数据", ArticleDetailReadLogsRespVO.class,
                        BeanUtils.toBean(list, ArticleDetailReadLogsRespVO.class));
    }

}